package com.example.coreshopapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.coreshopapp.R;

import java.util.ArrayList;
import java.util.List;

import bean.OrderBean;

public class PayAdapter extends RecyclerView.Adapter<PayAdapter.ViewHolder> {
    private List<OrderBean.PaymentListBean> payList = new ArrayList<>();

    public PayAdapter(List<OrderBean.PaymentListBean> payList) {
        this.payList = payList;
    }
    public void updatePay(List<OrderBean.PaymentListBean> payList) {
        this.payList = payList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public PayAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View view = View.inflate(parent.getContext(), R.layout.item_pospay_list, null);
        View view = LayoutInflater.from(parent.getContext()).inflate( R.layout.item_pospay_list, parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PayAdapter.ViewHolder holder, int position) {
        OrderBean.PaymentListBean payItem = payList.get(position);
        holder.paynameTv.setText(payItem.getScomment());
        holder.amountTv.setText(payItem.getPayAmount() +"");
        holder.payamtTv.setText(payItem.getPayAmount() +"");

        holder.payEdit.setOnClickListener(view -> {
            if (payEditClickListener != null) {
                payEditClickListener.onEditClick(position,payItem);
            }
        });

        holder.payDelete.setOnClickListener(view -> {
            if (payDeleteClickListener != null) {
                payDeleteClickListener.onDeleteClick(position,payItem);
            }
        });
    }

    @Override
    public int getItemCount() {
        return payList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView paynameTv,amountTv, payamtTv,payDelete,payEdit;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            paynameTv = itemView.findViewById(R.id.id_tv_payname);
            amountTv = itemView.findViewById(R.id.id_tv_amount);
            payamtTv = itemView.findViewById(R.id.id_tv_payamt);
            payDelete = itemView.findViewById(R.id.pay_delete);
            payEdit = itemView.findViewById(R.id.pay_edit);
        }
    }

    public interface PayDeleteClickListener {
        void onDeleteClick(int i,OrderBean.PaymentListBean payItem);
    }
    private PayDeleteClickListener payDeleteClickListener;

    public void setDeleteNewListener(PayAdapter.PayDeleteClickListener newListener) {
        this.payDeleteClickListener = newListener;
    }

    public interface PayEditClickListener {
        void onEditClick(int i,OrderBean.PaymentListBean payItem);
    }

    private PayEditClickListener payEditClickListener;

    public void setEditNewListener(PayAdapter.PayEditClickListener newListener) {
        this.payEditClickListener = newListener;
    }


}
